package com.zsl.order.adapter;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.special.ResideMenuDemo.R;
import com.zsl.bean.OrderBean;
import com.zsl.util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 工作管理主界面的适配器
 * 
 * @author zsl
 * @version 2014-8-20 17:20:06
 */
public class TodayOrderAdapter extends BaseAdapter {
	// 上下文
	public Context context;
	// 项目
	public List<OrderBean> maps;
	// 界面构造器
	LayoutInflater inflater;
	public Util util=new Util();

	// 图片缓存
//	private PicDownloadUtil downloadUtil = PicDownloadUtil.getPicDownloadUtil();
//	private Drawable drawable;
//	private Map<String, SoftReference<Drawable>> imageCache = downloadUtil
//			.getImageCache();

	public TodayOrderAdapter(Context context, List<OrderBean> maps) {
		super();
		this.context = context;
		this.maps = maps;
		this.inflater = LayoutInflater.from(context);
	}

	// 虚拟缓冲区类
	class ViewHolder {
		public ListView names;
		public TextView  money,time;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return maps.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return maps.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(
					R.layout.today_order_item, null);
			holder.names = (ListView) convertView
					.findViewById(R.id.today_order_item_listView1);
			holder.time = (TextView) convertView
					.findViewById(R.id.today_order_item_time);
			holder.money = (TextView) convertView
					.findViewById(R.id.today_order_item_money);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// // 设置文字和图片
		// final Map<String, Object> map = list_colleagueInfo.get(position);
		// if (map.get("photo").toString() != null
		// && !"".equals(map.get("photo").toString())) {
		//
		// Log.i("XMPP", "下载头像了");
		//
		// Log.i("XMPP_icon", "同行的图片" + map.get("photo").toString());
		// String url3 = AppClient.BASE_IMAGE_URL
		// + map.get("photo").toString();
		// if (imageCache.containsKey(url3)) {
		// SoftReference<Drawable> reference = imageCache.get(url3);
		// if (reference != null) {
		// drawable = reference.get();
		// holder.mycolleague_image.setImageDrawable(drawable);
		// }
		// } else {
		// new MyAsynctask(holder.mycolleague_image).execute(url3);
		// }
		//
		// }
		// 得到maps的item
//		OrderBean orderBean = maps.get(position);
		OrderBean bean=maps.get(position);
		String ordername=bean.getOrderName();
		List<Map<String, Object>> maps=util.getList(ordername);
		SimpleAdapter name=new SimpleAdapter(context, maps, R.layout.today_order_item_item, new String[]{"name"}, new int[]{R.id.today_order_item_item_name});
		ViewGroup.LayoutParams params = holder.names.getLayoutParams();
		String he=60.5*maps.size()+"";
		params.height=Integer.parseInt(new BigDecimal(he).setScale(0, BigDecimal.ROUND_HALF_UP)+"");
		holder.names.setLayoutParams(params);
		holder.names.setAdapter(name);
		name.notifyDataSetChanged();
		holder.money.setText(bean.getMoney());
		holder.time.setText(bean.getTime());
		

		return convertView;
	}

	// // 头像设置
	// class MyAsynctask extends AsyncTask<String, Void, Bitmap> {
	// private ImageView imageView;
	//
	// public MyAsynctask(ImageView imageView) {
	// this.imageView = imageView;
	// }
	//
	// @Override
	// protected Bitmap doInBackground(String... arg0) {
	// try {
	// URL url = new URL(arg0[0]);
	// HttpURLConnection connection = (HttpURLConnection) url
	// .openConnection();
	// InputStream in = connection.getInputStream();
	// Bitmap bitmap = BitmapFactory.decodeStream(in);
	// Bitmap bitmap2 = downloadUtil.createFramedPhoto(70, 70, bitmap,
	// AppClient.BASE_IMAGE_SCRE);
	// drawable = new BitmapDrawable(bitmap2);
	// imageCache.put(arg0[0], new SoftReference<Drawable>(drawable));
	// // imageView.setImageBitmap(btm2);
	// return bitmap2;
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return null;
	// }
	//
	// @Override
	// protected void onPostExecute(Bitmap result) {
	// // TODO Auto-generated method stub
	// super.onPostExecute(result);
	// imageView.setImageBitmap(result);
	// }
	//
	// }

}
